import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import java.util.Scanner;

public class GoodsManager{
    
    private static final String DB_NAME = "hw24";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "";
    
    Scanner scan;
    //与数据库连接的一个Connection
    Connection conn;
    //由conn创建Statement
    Statement stmt;
    //数据库字段名
    String[] columnNames;
    //数据库字段对应类型
    Class<?>[] columnClasses;
    
    public GoodsManager(){
        scan = new Scanner(System.in);
    }
    
    //启动商品管理器
    public void execute(){
        
        //连接数据库
        try{
            ConnectDB();            
        }catch(Exception e){
            e.printStackTrace();
        }
        
        boolean quit = false;
        while(!quit){
            //提示菜单
            System.out.println("1.插入");
            System.out.println("2.删除");
            System.out.println("3.修改");
            System.out.println("4.查询");
            System.out.println("5.退出");
            
            int choose = scan.nextInt();
            
            switch(choose){
                case 1: insertView(); break;
                case 2: deleteView(); break;
                case 3: updateView(); break;
                case 4: selectView(); break;
                case 5: quit = true;  break;
                default: break;
            }
        }
    }
    
    //连接数据库
    private ConnectDB() throws Exception {
        
        try{
            //装载Driver类
            Class.forName("com.mysql.jdbc.Driver");
            
            //连接数据库
            String url = "jdbc:mysql://localhost:3306/" + DB_NAME + "?useSSL=false";
            
            conn = DriverManager(url, USERNAME, PASSWORD);
            stmt = conn.createStatement();
            
            //获取goods库所有字段名
            String sql = "select * from " + DB_NAME + "limit 1";
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData rsmd = rs.getMetaData();
            
            int numberOfColumns = rsmd.getColumnCount();
            columnNames = new String[numberOfColumns];
            columnClasses = new Class<?>[numberOfColumns];
            
            for(int i = 0; i < numberOfColumns; i++){
                columnNames[i] = rs.getString(i + 1);
                columnClasses[i] = Class.forName(columnNames[i]);
            }
            
        }catch(Exception e){
            throw e;
        }      
    }
    
    /******************* 视图 ********************/
    
    //视图 - 插入
    private void insertView(){
        //提示菜单
        System.out.println("操作 - 插入");
        for(int i = 0; i < columnNames.length; i++){
            System.out.println(columnNames[i] + " : ");
            
        }
    }
    
    //视图 - 删除
    private void deleteView(){
        
    }
    
    //视图 - 修改
    private void updateView(){
        
    }
    
    //视图 - 查询
    private void selectView(){
        
    }
    
    /****************** 控制器 ******************/
    
    //控制器 - 插入
    private void insertController() throws Exception {
        
    }
    
    //控制器 - 删除
    private void deleteController() throws Exception {
        
    }
    
    //控制器 - 修改
    private void updateController() throws Exception {
        
    }
    
    //控制器 - 查询
    private void selectController() throws Exception {
        
    }
    
}